package net.wsfcp.android.http.mvp.model;

import android.support.annotation.NonNull;
import android.util.Log;

import com.google.gson.Gson;

import net.wsfcp.android.base.BaseApplication;
import net.wsfcp.android.base.BaseModel;
import net.wsfcp.android.bean.requestbean.ReqBillBean;
import net.wsfcp.android.bean.resultbean.BillBean;
import net.wsfcp.android.bean.resultbean.UserAssetsBean;
import net.wsfcp.android.http.UrlHelper;
import net.wsfcp.android.http.exception.ApiException;
import net.wsfcp.android.http.exception.ExceptionEngine;
import net.wsfcp.android.http.exception.ServerException;
import net.wsfcp.android.http.mvp.subscriber.CommonSubscriber;
import net.wsfcp.android.http.mvp.subscriber.ErrorSubscriber;
import net.wsfcp.android.http.mvp.transformer.CommonTransformer;
import net.wsfcp.android.utils.SpUtils;

import okhttp3.MediaType;
import okhttp3.RequestBody;


/**
 * Created by Shaoboyang on 2018/12/14.
 * 主要做一些数据处理呀,网路请求
 */

public class BillModel extends BaseModel {
    //账单列表
    public void getBillList(@NonNull int page, @NonNull int pageSize, @NonNull final BillListInfoHint
            infoHint) {

        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");

        ReqBillBean bean = new ReqBillBean(page, pageSize);
        Gson gson = new Gson();
        String obj = gson.toJson(bean);
        Log.e("json数据", obj);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj);
        httpService.getBillList(UrlHelper.GET_BILL_LIST + SpUtils.getInstance().getToken(), body).compose(new CommonTransformer())
                .subscribe(new CommonSubscriber<BillBean>(BaseApplication.getContext()) {
                    @Override
                    public void accept(BillBean loginBean) {
                        infoHint.billListSuccessInfo(loginBean);
                    }


                }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
                    @Override
                    public void accept(Throwable t) {
                        ServerException e = ExceptionEngine.handleException(t);
                        infoHint.billListFailInfo(e.code, e.message);

                    }
                });
    }

    //总资产
    public void getTotalAssets(@NonNull final GetTotalAssetsInfoHint
                                       infoHint) {

        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");

        httpService.getTotalAssets(UrlHelper.GET_TOTAL_ASSETS + SpUtils.getInstance().getToken()).compose(new CommonTransformer())
                .subscribe(new CommonSubscriber<UserAssetsBean>(BaseApplication.getContext()) {
                    @Override
                    public void accept(UserAssetsBean userAssetsBean) {
                        infoHint.getTotalAssetsSuccessInfo(userAssetsBean);
                    }


                }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
                    @Override
                    public void accept(Throwable t) {
                        ServerException e = ExceptionEngine.handleException(t);
                        infoHint.getTotalAssetsFailInfo(e.message);

                    }
                });
    }


    //通过接口产生信息回调
    public interface BillListInfoHint {
        //登录
        void billListSuccessInfo(BillBean loginBean);

        void billListFailInfo(int code, String str);


    }


    //通过接口产生信息回调
    public interface GetTotalAssetsInfoHint {
        //登录
        void getTotalAssetsSuccessInfo(UserAssetsBean bean);

        void getTotalAssetsFailInfo(String str);


    }

}
